package com.project.service.impl;

import com.project.bean.UserBean;
import org.apache.ibatis.session.SqlSession;
import com.project.bean.CutPageBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;

import java.time.LocalDate;
import java.util.List;

/**
 * @Title: UserServiceImpl
 * @Package: com.project.service
 * @Description:
 * @Author: Liang-Xie
 * @Date: 2022/7/13 - 17:31
 */
@SuppressWarnings("all")
public class UserServiceImpl extends BaseService implements IUserService {

    @Override
    public void add(UserBean userBean) {
        SqlSession session=this.getSession();
        IUserMapper mapper=session.getMapper(IUserMapper.class);
        mapper.add(userBean);
        session.commit();
        session.close();

    }

    @Override
    public void del(Integer id) {
        SqlSession session=this.getSession();
        IUserMapper mapper=session.getMapper(IUserMapper.class);
        mapper.del(id);
        session.commit();
        session.close();
    }

    @Override
    public void update(Integer id, String password, String rank) {
        SqlSession session=this.getSession();
        IUserMapper mapper=session.getMapper(IUserMapper.class);
        mapper.update(id,password,rank);
        session.commit();
        session.close();
    }

    @Override
    public UserBean check(String name, String password) {
        SqlSession session=this.getSession();
        IUserMapper mapper=session.getMapper(IUserMapper.class);
        UserBean userBean=mapper.check(name, password);
        return userBean;
    }

    @Override
    public List<UserBean> findByItem(String name, LocalDate startBirthday, LocalDate endBirthday) {
        SqlSession session=this.getSession();
        IUserMapper mapper=session.getMapper(IUserMapper.class);
        List<UserBean>userBeanList=mapper.findByItem(name, startBirthday, endBirthday);
        session.close();
        return userBeanList;

    }
    @Override
    public CutPageBean<UserBean> cutByItem(int pageNO, String name, LocalDate startBirthday, LocalDate endBirthday){
        SqlSession session=this.getSession();
        IUserMapper mapper=session.getMapper(IUserMapper.class);
        CutPageBean<UserBean>userBeanCutPageBean=new CutPageBean<>();
        userBeanCutPageBean.setList(mapper.cutList(name,startBirthday,endBirthday,(pageNO-1)*CutPageBean.PAGESIZE,CutPageBean.PAGESIZE));
        userBeanCutPageBean.setCount(mapper.cutCount(name, startBirthday, endBirthday));
        return userBeanCutPageBean;
    };

    @Override
    public UserBean findById(Integer id) {

        SqlSession session=this.getSession();
        IUserMapper mapper=session.getMapper(IUserMapper.class);
        UserBean userBean=mapper.findById(id);
        session.close();
        return userBean;
    }
}
